package client.dialog;

import javafx.animation.FadeTransition;
import javafx.geometry.Pos;
import javafx.scene.control.Label;
import javafx.scene.layout.StackPane;
import javafx.stage.Popup;
import javafx.stage.Stage;
import javafx.util.Duration;

public class MessageNotificationDialog {
    public static void showMessage(Stage owner, String text) {
        Label label = new Label(text);
        label.setStyle(
                "-fx-background-color: rgba(14,222,222,0.71); " +
                        "-fx-text-fill: white; " +
                        "-fx-padding: 10; " +
                        "-fx-background-radius: 5;"
        );

        StackPane content = new StackPane(label);
        content.setAlignment(Pos.CENTER);

        Popup popup = new Popup();
        popup.getContent().add(content);
        popup.setAutoHide(true);

        // 定位
        popup.show(owner,
                owner.getX() + owner.getWidth() - 200,
                owner.getY() + 40
        );

        // 淡入淡出
        FadeTransition fadeIn = new FadeTransition(Duration.millis(300), content);
        fadeIn.setFromValue(0);
        fadeIn.setToValue(1);

        FadeTransition fadeOut = new FadeTransition(Duration.millis(300), content);
        fadeOut.setFromValue(1);
        fadeOut.setToValue(0);
        fadeOut.setDelay(Duration.seconds(2)); // 停留 2 秒
        fadeOut.setOnFinished(e -> popup.hide());

        fadeIn.setOnFinished(e -> fadeOut.play());
        fadeIn.play();
    }
}
